//
//  Trough.m
//  SKPAD
//
//  Created by admin@amonline.com.cn on 15/5/4.
//  Copyright (c) 2015年 amo. All rights reserved.
//

#import "KnobGraduation.h"

@implementation KnobGraduation

-(id) init
{
    self = [super init];
    if (self) {
        _trackLayerGraduation = [CAShapeLayer layer];
        _trackLayerGraduation.fillColor = [UIColor clearColor].CGColor;
        _trackLayerGraduation.strokeColor = [UIColor blackColor].CGColor;
        _totalDuplicate = 30;
        
        _trackLayerReality = [CAShapeLayer layer];
        _trackLayerReality.fillColor = [UIColor clearColor].CGColor;
        UIColor * lightBlue = [UIColor colorWithRed:51.0/255 green:254.0/255 blue:255.0/255 alpha:1];
        _trackLayerReality.strokeColor = lightBlue.CGColor;
    }
    return self;
}

-(void) updateTrackLayerGraduation
{
    CGFloat totalAngle = _endAngle - _startAngle;
    CGFloat eachDuplicate = totalAngle / _totalDuplicate;

    //刻度线
    UIBezierPath * pointerOutside = [UIBezierPath bezierPath];
    for (int i = 0; i <= 30; i++)
    {
        CGFloat startX = _centerX + cosf(_startAngle + eachDuplicate*i) * _radiusInside;
        CGFloat startY = _centerY + sinf(_startAngle + eachDuplicate*i) * _radiusInside;
        [pointerOutside moveToPoint:CGPointMake(startX, startY)];
        
        CGFloat endX = _centerX + cosf(_startAngle + eachDuplicate*i) * _radiusOutside;
        CGFloat endY = _centerY + sinf(_startAngle + eachDuplicate*i) * _radiusOutside;
        [pointerOutside addLineToPoint:CGPointMake(endX,endY)];
    }
    self.trackLayerGraduation.path = pointerOutside.CGPath;
}

-(void)updateWithBounds:(CGRect)bounds
{
    //设置图层位置大小
    self.trackLayerGraduation.bounds = bounds;
    self.trackLayerGraduation.position = CGPointMake(CGRectGetWidth(bounds)/2.0, CGRectGetHeight(bounds)/2.0);
    self.trackLayerReality.bounds = bounds;
    self.trackLayerReality.position = CGPointMake(CGRectGetWidth(bounds)/2.0, CGRectGetHeight(bounds)/2.0);
    
    CGRect startRect = self.trackLayerGraduation.bounds;
    startRect = self.trackLayerReality.bounds;
    //图片大小和图标的偏移量
    _difference = startRect.size.width / 50.0;
    //外圈圆半径
    _radiusOutside = (startRect.size.width) / 2.0 - _difference;
    //内圈半径是外圈半径的5分之4
    _radiusInside = _radiusOutside * 0.85;
    //圆心坐标,相对屏幕位置的坐标
    _centerX = startRect.origin.x + startRect.size.width / 2;
    _centerY = startRect.origin.y + startRect.size.height / 2;
    
    [self updateTrackLayerGraduation];
}

-(void) setRealityAngle:(CGFloat)realityAngle
{
    _realityAngle = realityAngle;
    CGFloat totalAngle = _endAngle - _startAngle;
    CGFloat tempTotalRealityAngle = _realityAngle - _startAngle;
    CGFloat eachDuplicate = totalAngle / _totalDuplicate;

    //实际刻度线
    UIBezierPath * pointerInside = [UIBezierPath bezierPath];
    for(int i = 0; i <= 30; ++i)
    {
        //当刻度等于0的时候，说明没有值，不画第一条线
        if (_realityAngle == _startAngle)
        {
            continue;
        }
        //当便宜的总刻度大于实际刻度的时候跳出循环
        if (eachDuplicate * i > tempTotalRealityAngle)
        {
            break;
        }
        CGFloat startX = _centerX + cosf(_startAngle + eachDuplicate*i) * _radiusInside;
        CGFloat startY = _centerY + sinf(_startAngle + eachDuplicate*i) * _radiusInside;
        [pointerInside moveToPoint:CGPointMake(startX, startY)];
        
        CGFloat endX = _centerX + cosf(_startAngle + eachDuplicate*i) * _radiusOutside;
        CGFloat endY = _centerY + sinf(_startAngle + eachDuplicate*i) * _radiusOutside;
        [pointerInside addLineToPoint:CGPointMake(endX,endY)];
    }
    self.trackLayerReality.path = pointerInside.CGPath;
    
}
@end